﻿using Cxx.Entity;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Cxx.Common;
using Cxx.Entity.Extend;
using Cxx.IDAL;
using Dapper;

namespace Cxx.DAL
{
    public class StockDal : DataAccess , IStockDal
    {
        public void Insert(List<StockInfo> infos)
        {
            string sql =
                "INSERT INTO tblStock (Receiver,TradeName,TotalCount) VALUES(@Receiver,@TradeName,@TotalCount)";
            using (IDbConnection conn = GetConnection())
            {
                conn.Execute(sql, infos);
            }
        }

        public void Insert(StockInfo info)
        {
            string sql =
                "INSERT INTO tblStock (Receiver,TradeName,TotalCount) VALUES(@Receiver,@TradeName,@TotalCount)";
            using (IDbConnection conn = GetConnection())
            {
                conn.Execute(sql, info);
            }
        }

        public void Update(StockInfo info)
        {
            string sql =
                "UPDATE tblStock SET Receiver=@Receiver,TradeName=@TradeName,TotalCount=@TotalCount WHERE Id=@Id";
            using (IDbConnection conn = GetConnection())
            {
                conn.Execute(sql, info);
            }
        }

        public StockInfo Get(string receiver, string tradeName)
        {
            object para = new {Receiver = receiver, TradeName = tradeName};
            string sql = "SELECT * FROM tblStock WHERE Receiver=@Receiver AND TradeName=@TradeName ";
            using (IDbConnection conn = GetConnection())
            {
                return conn.QueryFirstOrDefault<StockInfo>(sql, para);
            }
        }

        public List<StockInfo> Get(string receiver)
        {
            object para = new {Receiver = receiver};
            string sql = "SELECT * FROM tblStock WHERE Receiver=@Receiver";
            using (IDbConnection conn = GetConnection())
            {
                return conn.Query<StockInfo>(sql, para)?.ToList();
            }
        }

        public List<StockInfo> GetListForPage(string condition, int pageIndex, int pageSize, out int recordCount)
        {
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("@_tables", "tblStock", DbType.String, null, 1000);
            //parameters.Add("@PK", "Id", DbType.String, null, 100);
            parameters.Add("@_orderby", "Id DESC", DbType.String, null, 200);
            parameters.Add("@_pageindex", pageIndex, DbType.Int32);
            parameters.Add("@_pageSize", pageSize, DbType.Int32);
            parameters.Add("@_fields", "*", DbType.String, null, 1000);

            #region 获取查询条件
            Hashtable fields = new Hashtable();
            var field = new DataFieldInfo("Receiver", "Character", "tblStock", "LIKE");
            fields.Add("txtReceiver", field);

            #endregion
            string sqlWhere = SqlFilter.Build(condition, fields);

            parameters.Add("@_where", sqlWhere, DbType.String, null, 1000);
            parameters.Add("@_totalcount", dbType: DbType.Int32, direction: ParameterDirection.Output);
            parameters.Add("@_pagecount", dbType: DbType.Int32, direction: ParameterDirection.Output);
            using (IDbConnection dbConnection = GetConnection())
            {
                var result = dbConnection.Query<StockInfo>(USP_PAGING_PROCEDURE, parameters,
                    commandType: CommandType.StoredProcedure);
                if (result == null)
                {
                    recordCount = 0;
                    return null;
                }
                recordCount = parameters.Get<int>("@_totalcount");
                return result.ToList();
            }
        }

        public void Delete(List<int> ids)
        {
            object para = new {Ids = ids};
            string sql = "DELETE FROM tblStock WHERE Id in @Ids";
            using (IDbConnection conn = GetConnection())
            {
                conn.Execute(sql, para);
            }
        }

        public void Delete(string receiver)
        {
            object para = new {Receiver = receiver};
            string sql = "DELETE FROM tblStock WHERE Receiver = @Receiver";
            using (IDbConnection conn = GetConnection())
            {
                conn.Execute(sql, para);
            }
        }
    }
}
